package com.jia.leetCode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: Leetcode
 * @description: 762. Prime Number of Set Bits in Binary Representation
 * @author: STU756
 * @create: 2020-08-16 21:36
 */
public class Pro0762 {
    //int [-2^31, 2^31-1]
    Set<Integer> set = new HashSet<>(Arrays.asList(2,3,5,7,11,13,17,19,23,29));
    public int countPrimeSetBits(int L, int R) {
        int ans = 0;
        for(int val = L; val <= R; val++) {
            if(isPrime(val)) {
                ++ans;
            }
        }
        return ans;
    }
    private boolean isPrime(int num){
        int countOne = 0;//统计1的个数
        while(num != 0) {
            num &=(num-1);
            ++countOne;
        }
        //计算countOne质数
        return set.contains(countOne);
    }
}
